package com.leetcode.dfs;

import java.util.ArrayList;
import java.util.List;

public class Permute {

    List<List<Integer>> res = new ArrayList<>();

    public List<List<Integer>> permute(int[] nums) {
        dfs(nums, new ArrayList<>(), new boolean[nums.length]);
        return res;
    }

    private void dfs(int[] nums, List<Integer> tempList, boolean[] isUsed) {
        if (tempList.size() == nums.length) {
            res.add(new ArrayList<>(tempList));
        }
        for (int i = 0; i < nums.length; i++) {
            if (isUsed[i]) {
                continue;
            }
            tempList.add(nums[i]);
            isUsed[i] = true;
            dfs(nums, tempList, isUsed);
            isUsed[i] = false;
            tempList.remove(tempList.size() - 1);
        }
    }
}
